Uec3.ReceiptAddressQueryWindow = Ext.extend(Ext.app.Module, {
	id : 'receiptAddressQuery',
	type : 'common/receiptAddressQuery',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {
	},
	paramsCollection : new Ext.util.MixedCollection(true),

	getParamsByKey : function(key) {
		return this.paramsCollection.get(key);
	},
	selectReceiptAddress : function(rowIndex) {

		var receiptAddress_grid = this.getParamsByKey('receiptAddress_grid');

		var selectModel = receiptAddress_grid.getSelectionModel();

		selectModel.selectRow(rowIndex);

		var record = selectModel.getSelected();

		this.setLookupValue(record.data, receiptAddress_grid.ownerCt);

		var lookup = receiptAddress_grid.ownerCt.lookup;
		
		if(lookup.lookupCallBackEvents){
       	  Ext.each(lookup.lookupCallBackEvents,function(event){
       		  if(event == 'lookupChange'){
       			 lookup.fireEvent(event,record);
       		  }else{
       			 lookup.fireEvent(event);
       		  }
       	  });
        }
		
		receiptAddress_grid.ownerCt.close();
		
	},

	getComponents : function(scope) {

		var receiptAddress_ds;
		var receiptAddress_grid;

		Ext.data.Store.prototype.applySort = function() {

			if (this.sortInfo && !this.remoteSort) {

				var s = this.sortInfo, f = s.field;
				var st = this.fields.get(f).sortType;
				var fn = function(r1, r2) {

					var v1 = st(r1.data[f]), v2 = st(r2.data[f]);
					if (typeof(v1) == "string") {

						return v1.localeCompare(v2);
					}
					return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);

				}
				this.data.sort(s.direction, fn);

				if (this.snapshot && this.snapshot != this.data) {

					this.snapshot.sort(s, direction, fn);

				}

			}

		};

		function renderDesc(value, cellmeta, record, rowIndex, columnIndex,
				store) {

			var str = "<a style='font-size:12px' href='javascript:Ext.CUR_APP.getModule(\"receiptAddressQuery\").selectReceiptAddress("
					+ rowIndex + ");'>选中</a>";

			return str;

		};

		var getAndReceiptAddressRestore = function() {


			receiptAddress_ds = new Ext.data.Store({
				// proxy: new Ext.data.MemoryProxy(myData),
				proxy : new Ext.data.HttpProxy({
							url : Uec3.App.Constants.homeUrl
									+ '/receiptAddress/edit_receiptAddress!query.do'
						}),
				baseParams : {
					start : 0,
					limit : 10
				},
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'root'
						}, [{
									name : 'desc'
								}, {
									name : 'id'
								}, {
									name : 'areaId'
								}, {
									name : 'companyId'
								}, {
									name : 'province'
								}, {
									name : 'city'
								}, {
									name : 'district'
								}, {
									name : 'street'
								}, {
									name : 'receiverName'
								}, {
									name : 'phone'
								}, {
									name : 'mobile'
								}, {
									name : 'address'
								}

						]),
				remoteSort : true,

				// 默认排序字段
				sortInfo : {
					field : "id",
					direction : "asc"
				},
				listeners:{
				  	'load':function(store,records,options){			  		
				  	     Ext.each(records,function(record,index,array){
				  	   	     var address = '地址:' + (record.data.province ? record.data.province : '') + (record.data.city ? record.data.city : '') + 
	  	   	             		(record.data.district ? record.data.district : '') + (record.data.street ? record.data.street : '') + 
	  	   	             		(record.data.receiverName ? ";联系人:" + record.data.receiverName : '') + 
	  	   	             		(record.data.phone ? ";电话:" + record.data.phone : '') + 
	  	   	             		(record.data.mobile ? ";手机:" + record.data.mobile : '');
					  	     record.set('address',address);
				  	     });
				     }
		    	}

			});

		};

		var receiptAddresscomponentInits = function() {

			// 初始化receiptAddress表格对象

			var receiptAddress_colModel = new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(),

					{
						header : "操作",
						width : 80,
						dataIndex : 'desc',
						align : 'center',
						renderer : renderDesc
					}, {
						header : "省",
						width : 60,
						dataIndex : 'province'
					}, {
						header : "市",
						width : 60,
						dataIndex : 'city'

					}, {
						header : "区",
						width : 60,
						dataIndex : 'district'

					}, {
						header : "联系人",
						width : 80,
						dataIndex : 'receiverName'
					}, {
						header : "电话",
						width : 80,
						dataIndex : 'phone'
					}, {
						header : "地址",
						width : 160,
						dataIndex : 'street'
					}]);

			// create the Grid
			receiptAddress_grid = new Ext.grid.GridPanel(

			{
						// title:'收货地址列表',
						region : 'center',
						iconCls : 'icon-grid',
						enableColumnMove : false,
						enableColumnMoveResize : false,
						stripeRows : true,
						height : 500,
						loadMask : {
							msg : "...加载数据中"
						},
						store : receiptAddress_ds,
						cm : receiptAddress_colModel,
						sm : new Ext.grid.RowSelectionModel({
									singleSelect : true
								}),
						viewConfig : {
							forceFit : true,
							columnsText : '显示的列',
							scrollOffset : 20,
							sortAscText : '升序',
							sortDescText : '降序'
						},
						bbar : new Ext.PagingToolbar({
									pageSize : 10,
									store : receiptAddress_ds,
									display : true,
									displayInfo : true,
									displayMsg : '第 {0} - {1} 条  共 {2} 条',
									emptyMsg : "没有记录",
									items : [/**
												 * '-', { pressed: true,
												 * enableToggle:true, text:
												 * 'Show Preview', cls:
												 * 'x-btn-text-icon details',
												 * toggleHandler: function(btn,
												 * pressed){ var view =
												 * receiptAddress_grid.getView();
												 * view.showPreview = pressed;
												 * view.refresh(); } }
												 */
									]
								})

					});

			//receiptAddress_ds.load();

		};

		var controlReceiptAddressEvent = function() {

			receiptAddress_grid.on('rowdblclick', function() {

						var me = this;

						var rowId = arguments[1];

						var record = me.getStore().getAt(rowId);

						scope.setLookupValue(record.data, me.ownerCt);

						var lookup = me.ownerCt.lookup;
		
						if(lookup.lookupCallBackEvents){
				       	  Ext.each(lookup.lookupCallBackEvents,function(event){
				       		  if(event == 'lookupChange'){
				       			 lookup.fireEvent(event,record);
				       		  }else{
				       			 lookup.fireEvent(event);
				       		  }
				       	  });
				        }
						
						me.ownerCt.close();

					})

			/**
			 * receiptAddress_ds.on('load', function() { var me = this;
			 * 
			 * var records = me.getRange(); Ext.each(records, function(record) {
			 * 
			 * var rowId = me.indexOf(record); Ext.get('selectReceiptAddress_' +
			 * rowId) .on('click', function() {
			 * 
			 * selectReceiptAddress(rowId);
			 * 
			 * });
			 * 
			 * });
			 * 
			 * });
			 */
		};
		getAndReceiptAddressRestore();

		receiptAddresscomponentInits();

		controlReceiptAddressEvent();
		this.paramsCollection.add("receiptAddress_ds", receiptAddress_ds);
		this.paramsCollection.add("receiptAddress_grid", receiptAddress_grid);

		return receiptAddress_grid;

	},
	initEvent:function(){
		var module=this;
		var win=module.getParamsByKey("win");
		win.on('show',function(){
			this.getParamsByKey("receiptAddress_ds").load({
				 callback: function(records,options,success){
		         
		         if(success){
		        	 
		        	// Ext.Msg.alert('提示',"加载数据成功");
		         }else{
		        	 
		        	 
		        	 Ext.Msg.alert('提示',"网络连接异常...请稍候再试");
		         }			  
			
		 }
	
	});
		},module,{single:true});
	},
	/**
	 * Create this modules window here.
	 */
	createWindow : function() {
		var desktop = this.app.getDesktop();
		var module=this;
		var win = desktop.getWindow('receiptAddressQuery');
		if (!win) {
			win = desktop.createWindow({
						id : 'receiptAddressQuery',
						title : '收货地址列表',
						width : 600,
						height : 330,
						iconCls : 'acc-icon',
						shim : false,
						animCollapse : false,
						modal : true,
						constrainHeader : true,
						resizable : false,
						maximizable : false,
						taskbuttonTooltip : "收货地址列表",
						layout : "fit",
						items : [this.getComponents(this)]
					});	
			  module.paramsCollection.add("win",win);
			  module.initEvent();

		}
		win.show();

	}
});